package sample;

import annotation.ClassXpath;
import annotation.FieldXpath;
import bean.DownloadParams;
import bean.FetchParams;
import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import spider.Spider;
import spider.Spider_MQ;

import java.io.Serializable;


/**
 * Created by chen_ on 8/17/2017.
 */
public class ZhihuTXSpider {
    public static Spider_MQ spider = new Spider_MQ();

    @Data
    @ClassXpath(value = "//span[@class=UserLink]")
    public static class ZhihuUser implements Serializable {
        @FieldXpath("//a[@class=UserLink-link]/@href")
        private String url;// 标题
        @FieldXpath("//img[@class=UserLink-avatar]/@src")
        private String imgUrl;// 分类
    }

    private static void init() {
        spider.setTask0(() -> {
            DefaultMQProducer producer = new DefaultMQProducer(String.valueOf(0));
            producer.setNamesrvAddr(spider.NAME_SRV_ADDR);
            try {
                producer.start();
            } catch (MQClientException e) {
                e.printStackTrace();
            }
            for (int i = 1; i < 4840; i++) {
                Message msg = new Message("Topic" + 0 /* Topic */,
                        "TagA" /* Tag */,
                        ("https://www.zhihu.com/people/rednaxelafx/followers?page=" + i).getBytes() /* Message body */
                );
                try {
                    producer.send(msg);
                } catch (MQClientException | MQBrokerException e) {
                    e.printStackTrace();
                } catch (RemotingException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        if (args.length == 0) {
            init();
        }
        spider.chain(url ->
                FetchParams.builder().url((String) url).flag(FetchParams.Flag.dynamic).clazz(ZhihuUser.class).build()
        ).print().save(ZhihuUser.class).download(
                zhihuUser -> {
                    String imgUrl = ((ZhihuUser) zhihuUser).getImgUrl();
                    return DownloadParams.builder().filename("知乎头像/" + StringUtils.substringAfterLast(("" + imgUrl).replace("_im", ""), "/"))
                            .url(("" + imgUrl).replace("_im", "")).build();
                }
        ).end();
    }
}